/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bittorentlike;

import Pojo.PeerPojo;
import Util.Utils;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import define.Define;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import sun.security.util.Length;

/**
 *
 * @author Xuan Cau
 */
public class Send
{
    private int status = 0;
    private DatagramSocket serverSocket;
    public Send(){
        //ReadFileConfig();
        try
        {
             serverSocket = new DatagramSocket();
        }
        catch(Exception ex)
        {
            
        }
    }
    
    
    public void SendHello()
    {
        status = Define.HELLO;
        int len = 1;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.HELLO);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondHello(byte[] data, InetAddress ip, int port)
    {
        status = Define.RESPOND_HELLO;
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_HELLO);
        buf.put(data);
        byte[] data_send = buf.array();
        SendReponeRequest(data_send, ip, port);
    }
    public void SendCofig(int port)
    {
        status = Define.CONFIG;
        int len = 4;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.CONFIG);
        buf.putInt(port);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondConfig()
    {
        
    }
    public void SendSearch()
    {
        status = Define.SEARCH;
        int len = 1;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.SEARCH);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondSearch(byte[] data, InetAddress ip, int port)
    {
        status = Define.RESPOND_SEARCH;
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_SEARCH);
        buf.put(data);
        byte[] data_send = buf.array();
        SendReponeRequest(data_send, ip, port);
    }
    public void SendDownload(byte[] chunk)
    {
        status = Define.DOWNLOAD;
        int len = 1 + chunk.length + 2;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.DOWNLOAD);
        buf.putShort((short)chunk.length);
        buf.put(chunk);
        byte[] data = buf.array();
        SendRequest(data);
    }
    public void SendRespondDownload(byte[] data, InetAddress ip, int port)
    {
        status = Define.RESPOND_DOWNLOAD;
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_DOWNLOAD);
        buf.put(data);
        byte[] data_send = buf.array();
        SendReponeRequest(data_send, ip, port);
    }
    public void SendDownloadTorrent(byte[] data)
    {
        status = Define.DOWNLOAD_TORRENT;
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.DOWNLOAD_TORRENT);
        buf.put(data);
        byte[] data_send = buf.array();
        SendRequest(data_send);
    }
    public void SendRespondDownloadTorrent(byte[] data, InetAddress ip, int port)
    {
        status = Define.RESPOND_DOWNLOAD_TORRENT;
        int len = 1 + data.length;
        ByteBuffer buf = ByteBuffer.allocate(4 + len);
        buf.putInt(len);
        buf.put((byte)Define.RESPOND_DOWNLOAD_TORRENT);
        buf.put(data);
        byte[] data_send = buf.array();
        SendReponeRequest(data_send, ip, port);
    }
    public void SendRequest(byte[] data)
    {
        try    
        {
           
           String Broadcastaddress = new String("255.255.255.255");
           int port = Define.PORT;         
           serverSocket.setBroadcast(true);
           InetAddress IPAddress = InetAddress.getByName(Broadcastaddress);
           
           for(int i=0; i< Utils.arr_peer.length; i++)
           {
                PeerPojo peer = new PeerPojo();
                peer = Utils.arr_peer[i];
                DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddress, peer.getPort());
                try {
                    Utils.socket.send(sendPacket);
                    //serverSocket.send(sendPacket);
                    System.out.println(IPAddress);
                    System.out.println(peer.getPort());
                } catch (Exception ex1) {
                    System.out.println(ex1.toString());
                }
              
           }
        }
        catch(Exception ex)
        {
           System.out.println(ex);
        }
    }
    public void SendReponeRequest(byte[] data, InetAddress ip, int port)
    {
        System.out.println("ip = " +ip + "port = " + port);
        try
        {       
            DatagramPacket sendPacket = new DatagramPacket(data, data.length, ip, port);
            try {
                Utils.socket.send(sendPacket);
               
            } catch (Exception ex1) {
                System.out.println(ex1.toString());
            }
              
           
        }
        catch(Exception ex)
        {
            
        }
    }
    public void SendFileTorrent(String fileName)
    {   
        
        try
        {
            File file;
            file = new File(fileName);
            byte[] name = Utils.getFileName(fileName).getBytes();
             
            FileInputStream is = new FileInputStream(file);
            DataInputStream in = new DataInputStream(is);
            byte[] data_file = new byte[(int)file.length()];

            in.read(data_file);
            status = Define.UPLOAD;
            int len = 1 + data_file.length + 2 + name.length + 2;          
            ByteBuffer buf = ByteBuffer.allocate(6 + len);
            buf.putInt(len);
            buf.put((byte)Define.UPLOAD);
            buf.putShort((short)name.length);
            buf.put(name);
            buf.putShort((short)data_file.length);
            buf.put(data_file);
            byte[] data_send = buf.array();
            SendRequest(data_send);
            
        }
        catch(Exception ex)
        {

            System.out.println(ex);
        }
        
        
    }
}
